<template>
	<om-addpage style="padding-top: 35px;" :busCode="busCode" :menuName="menuName" @formSubmit="formSubmit">
		<om-autoform ref="autoform" :initConfigData="mainConfigData" :initValue="initValue"></om-autoform>
	</om-addpage>
</template>

<script>
	import {
		errorModal,
		ignoreHtml,
		getUrl,
		confirmModal,
		ptFloat,
		minusF
	} from '@/static/js/common.js';
	import {
		changeConfigAttr,
		buildFormData,
		formValidation,
		tranferToEditors,
		changeEditor,
		setSubmitSubValue,
		configSelectData,
		hasValue,
		formShowLoading,
		getFormFields,
		formModelLoad,
		getWorkCacheKey
	} from '@/static/js/formUtil.js';
	import urlUtil from '@/static/js/urlUtil.js';
	import { mapState } from 'pinia';
	import {useLoginUserStore} from '@/stores/login-user.js';
	export default {
		provide() {
			return {
				formPage: this
			};
		},
		data() {
			return buildFormData({
				code:"",
				InstCacheKey:"pm-yy-tindexEvaluate-inst-cache"
			})
		},
		computed: {
			...mapState(useLoginUserStore, ['userName', 'userId'])
		},
		methods:{
			formSubmit(){
				let _that = this
				_that.om.formSave("tindexEvaluate/addTindexEvaluate",_that.formdata,_that)
			},
			
			getDatas(){
				let _that = this
				_that.om.request(
					"tindexEvaluate/getDatas",
					{
						year:_that.$refs.autoform.formdata.year,
						instId:_that.$refs.autoform.formdata.instId
					}
				).then(res=>{
					errorModal(_that, '提示信息', " 加载了" + (res.value.list.length || 0 )+ "条信息")
					_that.subEditorObj["titleInfo"].workList = res.value.list
					_that.$refs.autoform.changeConfigAttr("titleInfo", "length", res.value.list.length || 0)
					
				})
			},
			
			verifyInst1(id){
				let _that = this
				_that.om.request(
					"tindexEvaluate/verifyInst",
					{
						year:_that.$refs.autoform.formdata.year,
						instId:id
					}
				).then(res=>{
					if(res.value.tip){
						errorModal(_that,"提示信息",ignoreHtml(res.value.tip),function(){
						})
					}
				})
			},
			verifyInst(){
				let _that = this
				if(!_that.$refs.autoform.formdata.year){
					_that.om.toast("请先选择年份")
					return false
				}else if(_that.$refs.autoform.formdata.evaluateItemCatCode === "37675" && !_that.$refs.autoform.formdata.monthCode){
					_that.om.toast("请先选择月份")
					return false 
				}else{
					return true
				}
			},
			onSelectMonth(){
				let _that = this
				_that.$refs.autoform.changeEditor("instInfo","instId",{
					link:"../../public/pageSearch/pageSearch",
					"linkParam": {
						other: JSON.stringify({
							containBoard: true
						}),
						method: "post",
						namefield: 'instName',
						searchfield: "instName",
						initload: true,
						url: 'tinstitution/findNotBoard1',
						cachename: _that.InstCacheKey,
						placeholder: "请输入部门名称"
					}
				})
			},
			onSelectZpos(item){
				let _that = this
				_that.$refs.autoform.formdata.instId = item.zinstId
				_that.$refs.autoform.formdata.instName = item.zinstName
			},
			onSelectYear(item){
				let _that  = this
				let monthList = [{text:"请选择"}]
				_that.om.request(
					"dictZero/getListByCode",
					{
						code:"35803"
					}
				).then(res=>{
					if(_that.$refs.autoform.formdata.evaluateItemCatCode !== "37676"){
						for(let item of res){
							let obj =  {
								attr:{},
								text:"",
								value:"",
							}
							obj.attr = item
							obj.text = item.dicname
							obj.value = item.code
							monthList.push(obj)
							_that.$refs.autoform.configData[0].editors[3].prompt = '请选择'
							_that.$refs.autoform.configData[0].editors[3].data = monthList
							_that.$refs.autoform.configData[0].editors[3].type ="selectbox"
							_that.$refs.autoform.configData[0].editors[3].onSelect ="onSelectMonth"
						}
					}else{
						_that.onSelectMonth()
					}
				})
			},
			onSelectEvaluateItemCat(item){
				let _that = this
				_that.$refs.autoform.clear("topinfo","year")
				let date = new Date()
				let prevYear = Number(date .getFullYear())-1
				let nextYear = Number(date .getFullYear())
				let yearList = [{text:"请选择"},{attr:{id:prevYear,dicname:prevYear},text:prevYear,value:prevYear},{attr:{id:nextYear,dicname:nextYear},text:nextYear,value:nextYear}]
				_that.$refs.autoform.configData[0].editors[2].prompt = '请选择'
				_that.$refs.autoform.configData[0].editors[2].data = yearList
				_that.$refs.autoform.configData[0].editors[2].type ="selectbox"
				_that.$refs.autoform.configData[0].editors[2].onSelect ="onSelectYear"
				if(item.code === "37676"){
					_that.$refs.autoform.configData[0].editors[3].type ="labelbox"
				}
			},
			initLoad(options){
				let _that = this
				_that.code = options.code
				let ajaxArr = [
					urlUtil.zposUrl(options.code),
					urlUtil.dictZeroComboboxUrl("37674")
				]
				formModelLoad(ajaxArr,function(res,mainData){
					changeEditor('topinfo', 'zposId', 'data', configSelectData(res[0], 'id', 'posname'),mainData);
					changeEditor("topinfo","evaluateItemCatCode","data",configSelectData(res[1],"code","dicname"),mainData)
					if(res[0][0]){
						changeEditor("topinfo","zinstId",{
							defaultName:res[0][0].instName,
							defaultValue:res[0][0].instId
						},null,mainData)
					}
					changeConfigAttr("titleInfo", "showLength", true, mainData)
					if(_that.subEditorObj["titleInfo"]){
						changeEditor("titleInfo","objStr",{
							showAdd: false,
							showDel: false,
							showArrow: false,
							length: 0,
							showLength: true,
							titleTemplate:"代码：<%=orderNum || '未填写'%>|指标名称:<%=indexName || '未填写'%>",
							subtitle1Template:"姓名:<%=userName || '未填写'%>|岗位名称:<%=posName || '未填写'%>|运营范围:<%=busRange || '未填写'%>",
							subtitle2Template:"考核期目标值:<%=targetValueName || '未填写'%>|考核期执行数:<%=completeValueName || '未填写'%>|考核标识:<%=checkFlag || '未填写'%>",
							subtitle3Template:"权重(%):<%=weightScore || '未填写'%>|得分:<%=score || '未填写'%>|计分规则:<%='未填写'%> "
						},null,mainData)
					}
				},options)
			}
		},
		onLoad(options) {
			let _that = this
			_that.initLoad(options)
		},
		onShow() {
			let _that = this
			if(_that.$refs.autoform){
				uni.getStorage({
					key:_that.InstCacheKey,
					success:(res)=>{
						if(res.data){
							let d = res.data
							let formdata = _that.$refs.autoform.formdata
							formdata.instId = d.id
							formdata.instName = d.instName
							_that.verifyInst1(d.id)
							_that.getDatas()
							uni.removeStorage({
								key:_that.InstCacheKey
							})
						}
					}
				})
			}
		}
	}
</script>

<style></style>                                                                                                                                                                                                                                                                                                                                      
